package com.he.flyweightmodel.flyweightfactory;

import com.he.flyweightmodel.flyweight.Shap;
import com.he.flyweightmodel.flyweight.concreteflyweiight.CircleShap;

import java.util.HashMap;
import java.util.Map;

/**
 * 享元模式：所谓享元模式就是运行共享技术有效地支持大量细粒度对象的复用.<br/>
 * 享元模式的核心在于享元工厂类，享元工厂类的作用在于提供一个用于存储享元对象的享元池，用户需要对象时，首先从享元池中获取，
 * 如果享元池中不存在，则创建一个新的享元对象返回给用户，并在享元池中保存该新增对象.
 */
public class Flyweightfactory {

    static Map<String, Shap> map = new HashMap<>();

    public static Shap getShap(String key) {
        Shap shap1 = map.get(key);
        if (shap1 == null) {
            CircleShap shap = new CircleShap(key);
            map.put(key, shap);
            return shap;
        }
        return shap1;
    }
}
